package org.dreamwork.jasmine2.sample.ajax;

import org.dreamwork.jasmine2.ajax.RequestMethod;
import org.dreamwork.jasmine2.annotation.AjaxMethod;
import org.dreamwork.jasmine2.annotation.AjaxParameter;
import org.dreamwork.jasmine2.annotation.AjaxService;
import org.dreamwork.jasmine2.sample.converter.Class2;
import org.dreamwork.jasmine2.sample.converter.Test;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-12-10
 * Time: 17:04:32
 */
@AjaxService("test")
public class TestAjaxService {
    @AjaxMethod
    public void ajaxService1 () {
    }

    @AjaxMethod
    public void ajaxService2 (
        @AjaxParameter (name="p1") String p1,
        @AjaxParameter (name="date") Date date,
        @AjaxParameter (name="array", converter= ArrayConverter.class) int[] array) {
    }

    @AjaxMethod(method= RequestMethod.POST)
    public String getList (@AjaxParameter (name="size") int size) {
        StringBuilder builder = new StringBuilder ();
        for (int i = 0; i < size; i ++) {
            if (i != 0) builder.append (',');
            builder.append ("{name:'item ").append (i + 1).append ("', value:").append ((int)(Math.random () * 100)).append ("}");
        }
        return "[" + builder + ']';
    }

    @AjaxMethod(method = RequestMethod.POST)
    public String hello (@AjaxParameter (name="name") String name) {
        return "Hello " + name + "!";
    }

    @AjaxMethod (method = RequestMethod.POST)
    public Test getTest () {
        Test test = new Test ();
        test.setName ("中文测试");
        Map<String, String> map = new HashMap<String, String> ();
        map.put ("item_0", "value 0");
        test.setMap (map);

        List<Class2> list = new ArrayList<Class2> ();
        for (int i = 0; i < 3; i ++) {
            Class2 c2 = new Class2 ();
            c2.setId (i + 1);
            c2.setQty (Math.random () * 10000);
            c2.setType ((short) (Math.random () * 10));
            c2.setFlag (c2.getType () >= 5);
            list.add (c2);
        }
        test.setSecond (list);

        return test;
    }

    @AjaxMethod(method = RequestMethod.POST)
    public Test convert (
            @AjaxParameter (name = "test", serialize = true)
            Test test) {
        return test;
    }

    @AjaxMethod (method = RequestMethod.POST)
    public void processArray (@AjaxParameter (name="array", serialize = true) String[] array) {
        System.out.println (Arrays.toString (array));
    }

    @AjaxMethod (method = RequestMethod.POST)
    public void processList (@AjaxParameter (name="list", serialize = true) ArrayList<String> list) {
        System.out.println (list);
    }

    @AjaxMethod (method = RequestMethod.POST)
    public void testLong (@AjaxParameter (name="id")Long id) {
        System.out.println (id);
    }
}